﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Yeap.Config;
using Yeap.Kernel.Utils.Enums;

namespace Yeap.Kernel.Storage
{
    public partial class Work
    {
        private ISiteRepository siteRepository = new SiteRepository();

        public WorkTypes GetWorkType()
        {
            Int32 currentYear;
            try
            {
                currentYear = Convert.ToInt32(siteRepository.GetSetting(Constants.SiteSettingKeys.CurrentYear));    
            }
            catch( OverflowException)
            {
                throw new Exception(Constants.ExceptionMessages.SiteSettingOverflow);
            }
            catch( FormatException)
            {
                throw new Exception(Constants.ExceptionMessages.InvalidSiteSettingIntValue);
            }
            
            if (currentYear != Year)
            {
                return WorkTypes.PastYears;
            }

            if (this.UserWorks.Any(uw => uw.Type == AssignmentTypes.Finished))
            {
                return WorkTypes.Completed;
            }

            if (this.UserWorks.Any(uw => uw.Type == AssignmentTypes.Agreement))
            {
                return WorkTypes.InProgress;
            }
            return WorkTypes.Available;
        }

        public List<Work> GetHistory()
        {
            var result = new List<Work>();
            var currWork = this;
            while (currWork.Work2 != null)
            {
                currWork = currWork.Work2;
                result.Add(currWork);
            }
            return result;

        }
    }
}